package com.myBlog.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.myBlog.domain.ResponseResult;
import com.myBlog.domain.dto.CategoryDTO;
import com.myBlog.domain.entity.Category;
import com.myBlog.domain.vo.ExcelCategoryVo;
import com.myBlog.domain.vo.PageVo;
import com.myBlog.enums.AppHttpCodeEnum;
import com.myBlog.service.CategoryService;
import com.myBlog.utils.BeanCopyUtils;
import com.myBlog.utils.WebUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/content/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 查询所有分类接口
     *
     * @return
     */
    @GetMapping("/listAllCategory")
    public ResponseResult listAllCategory() {
        return ResponseResult.okResult(categoryService.listAllCategory());
    }

    /**
     * 分页条件查询
     *
     * @param pageNum
     * @param pageSize
     * @param categoryDTO
     * @return
     */
    @GetMapping("/list")
    public ResponseResult<PageVo> list(Integer pageNum, Integer pageSize, CategoryDTO categoryDTO) {
        return categoryService.list(pageNum, pageSize, categoryDTO);
    }

    /**
     * 添加分类
     *
     * @param categoryDTO
     * @return
     */
    @PostMapping
    public ResponseResult addCategory(@RequestBody CategoryDTO categoryDTO) {
        return categoryService.addCategory(categoryDTO);
    }

    /**
     * 根据id删除类别
     *
     * @param ids
     * @return
     */
    @DeleteMapping("{id}")
    public ResponseResult delete(@PathVariable("id") List<Long> ids) {
        return categoryService.deleteByIds(ids);
    }

    /**
     * 根据分类id查询分类信息
     *
     * @param categoryId
     * @return
     */
    @GetMapping("{id}")
    public ResponseResult getCategoryInfo(@PathVariable("id") Long categoryId) {
        return categoryService.getCategoryInfo(categoryId);
    }

    /**
     * 修改分类
     *
     * @param categoryDTO
     * @return
     */
    @PutMapping
    public ResponseResult updateCategory(@RequestBody CategoryDTO categoryDTO) {
        return categoryService.updateCategory(categoryDTO);
    }

    /**
     * Excel导出功能
     *
     * @param response
     */
    @PreAuthorize("@ps.hasPermission('content:category:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        //设置下载文件的请求头
        try {
            WebUtils.setDownLoadHeader("分类.xlsx", response);
            //获取需要导出的数据
            List<Category> categories = categoryService.list();
            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.copyBeanList(categories, ExcelCategoryVo.class);

            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class)
                    .autoCloseStream(Boolean.FALSE)
                    .sheet("分类导出")
                    .doWrite(excelCategoryVos);
        } catch (Exception e) {
            //如果出现异常也要响应JSON
            response.reset();
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }
}
